package com.taiyuan.tongcheng.adapter;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.utils.KJLoger;
import org.kymjs.kjframe.utils.StringUtils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taiyuan.tongcheng.application.TCBroadcastAction;
import com.taiyuan.tongcheng.bean.ServerStation;
import com.taiyuan.tongcheng.config.TcConfig;
import com.taiyuan.tongcheng.ui.MarkActivity;
import com.taiyuan.tongcheng.ui.OrderCancelActivity;
import com.taiyuan.tongcheng.widget.dialog.TcBuilder;
import com.tytc56.user.R;

public class HalfStationAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private int a=0;
	private Context context;
	private List<ServerStation> stationList=new ArrayList<ServerStation>();
    private Intent refreshUiIntent;
    private TcBuilder  tcInformDialog;
    public static final String TAG_TUJIN_POSITON="TAG_TUJIN_POSITON"; 
	public HalfStationAdapter(Context context, List<ServerStation> stationList) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.stationList=stationList;
		refreshUiIntent=new Intent(TCBroadcastAction.UI_REFREASH_TUJIN);
		tcInformDialog=TcBuilder.getInstance(context,1);
		tcInformDialog.setCancelable(false);
	}

	public int getCount() {
		return stationList.size();
	}

	public Object getItem(int position) {
		return stationList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ZuJian zuJian = null;
		OnClick click = null;
		if (convertView == null) {
			zuJian = new ZuJian();
			convertView = layoutInflater.inflate(R.layout.listview_half_station,
					null);
			zuJian.staInfoRl=(RelativeLayout)convertView.findViewById(R.id.rl_half_info);
			zuJian.staAddress=(TextView)convertView.findViewById(R.id.tv_half_add);
			zuJian.staConnectUser=(TextView)convertView.findViewById(R.id.tv_half_user);
			zuJian.staTel=(TextView)convertView.findViewById(R.id.tv_half_tel);
			zuJian.staHint=(TextView)convertView.findViewById(R.id.tv_half_hint);
			click=new OnClick();
			zuJian.deleteBtn=(Button)convertView.findViewById(R.id.btn_half_delete);
			zuJian.deleteBtn.setOnClickListener(click);
			convertView.setTag(zuJian);
			convertView.setTag(zuJian.deleteBtn.getId(),click);
		} else {
			zuJian = (ZuJian) convertView.getTag();
			click= (OnClick)convertView.getTag(zuJian.deleteBtn.getId());
		}
		if(StringUtils.isEmpty(stationList.get(position).getPlace())){
			zuJian.staInfoRl.setVisibility(View.GONE);
			zuJian.staHint.setVisibility(View.VISIBLE);
		}else{
			zuJian.staInfoRl.setVisibility(View.VISIBLE);
			zuJian.staHint.setVisibility(View.GONE);
			zuJian.staAddress.setText(stationList.get(position).getPlace());
			zuJian.staConnectUser.setText(stationList.get(position).getReceiver());
			zuJian.staTel.setText(stationList.get(position).getReceiverMobile());
		}
		click.setPosition(position);
		return convertView;
	}

	//列表按钮点击
	class OnClick implements OnClickListener {
		int position;
		public void setPosition(int position) {
			this.position = position;
		}
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_half_delete:
				tcInformDialog.setButton1Click(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {

						refreshUiIntent.putExtra(TAG_TUJIN_POSITON, position);
						context.sendBroadcast(refreshUiIntent);
						tcInformDialog.cancel();
					}
				}).setButton2Click(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						tcInformDialog.cancel();
					}
				}).withTitle("温馨提示").withContent("确定要删除该途径点么？").show();
				
				
				break;
			default:
				break;
			}
		}
	}
	
	
	public final class ZuJian {
		public RelativeLayout  staInfoRl;
		public TextView staAddress;
		public TextView staConnectUser;
		public TextView staTel;
		public TextView staHint;
		public Button  deleteBtn;
	}
	
	
	

}